package com.zhiwang.xiaoqu.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.zhiwang.xiaoqu.R
import sp
import java.lang.IllegalStateException

class UpdateProgressBar : View {

    //进度指示图标
    lateinit var progressLogo: Bitmap


    //进度起始颜色
    var startColor: Int = 0

    // 进度终止颜色
    var endColor: Int = 0

    //进度条背景色
    var progressBackgroundColor: Int = 0


    var progress: Int = 0
        set(value) {
            field = if (value >= 100) 100 else value
            invalidate()
        }

    val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    var gradientShader: LinearGradient? = null

    var textColor: Int = Color.WHITE

    var translateMatrix = Matrix()

    var txtMinWidth = 0f

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UpdateProgressBar)

        val logoResId = typedArray.getResourceId(R.styleable.UpdateProgressBar_up_logo, 0)

        if (logoResId == 0)
            throw  IllegalStateException("update logo resource is not set")

        progressLogo = BitmapFactory.decodeResource(context.resources, logoResId)

        startColor = typedArray.getColor(R.styleable.UpdateProgressBar_up_start_color, Color.BLACK)

        endColor = typedArray.getColor(R.styleable.UpdateProgressBar_up_end_color, Color.BLACK)

        progressBackgroundColor = typedArray.getColor(R.styleable.UpdateProgressBar_up_background_color, Color.WHITE)

        paint.textSize = 12.sp(context)

        typedArray.recycle()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        setMeasuredDimension(width, Math.max(progressLogo.height, MeasureSpec.getSize(heightMeasureSpec)))
        if (gradientShader == null)
            gradientShader = LinearGradient(
                0f,
                measuredHeight / 2f,
                measuredWidth.toFloat(),
                measuredHeight / 2f,
                startColor,
                endColor,
                Shader.TileMode.CLAMP
            )

        if (txtMinWidth == 0f) {
            txtMinWidth = paint.measureText("0%")
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val translateX = 0f

        translateMatrix.postTranslate(translateX, 0f)


        val txt = "$progress%"

        val txtWidth = paint.measureText(txt)

        val x =
            txtMinWidth + measuredHeight / 2f + (measuredWidth - measuredHeight - txtMinWidth) / 100f * progress - txtWidth

        paint.shader = null
        paint.color = progressBackgroundColor

        canvas?.drawRoundRect(
            0f,
            0f,
            measuredWidth.toFloat(),
            measuredHeight.toFloat(),
            measuredHeight / 2f,
            measuredHeight / 2f,
            paint
        )

        paint.shader = gradientShader
        val progressBarEnd = x + measuredHeight / 2f + txtWidth
        canvas?.drawRoundRect(
            0f,
            0f,
            progressBarEnd,
            measuredHeight.toFloat(),
            measuredHeight / 2f,
            measuredHeight / 2f,
            paint
        )

        paint.shader = null
        paint.color = textColor

        val fontMetrics = paint.fontMetrics
        val baseline = measuredHeight / 2f - fontMetrics.bottom / 2f - fontMetrics.top / 2f

        canvas?.drawText(txt, 0, txt.length, x, baseline, paint)

        canvas?.drawBitmap(
            progressLogo,
            progressBarEnd,
            (measuredHeight - progressLogo.height) / 2f,
            paint
        )

    }


}